package com.devnn.baseNet;
import com.devnn.dao.NameValue;
import com.devnn.util.MD5;
import com.squareup.okhttp.Call;
import com.squareup.okhttp.FormEncodingBuilder;
import com.squareup.okhttp.MediaType;
import com.squareup.okhttp.MultipartBuilder;
import com.squareup.okhttp.OkHttpClient;
import com.squareup.okhttp.Request;
import com.squareup.okhttp.RequestBody;
import com.squareup.okhttp.Response;
import java.io.File;
import java.io.IOException;
import java.util.List;
import java.util.concurrent.TimeUnit;

/**
 * Created by devnn on 16/1/18.
 */
public class BaseRequest {
    private OkHttpClient okHttpClient;
    private static String privateKey = "i5KppyKnGinSGGhFrVBsJz5XQhI14lmdel+QdN5DRkp7PlsdXFON3+4mpL21Ep8TgkT6iD";
    private static String publicKey = "vs1KJnwKu6BUnFO9MWvpORfMhNJkMzc4dZyjd1NaKRRWiY55tBIo3MBBrwOjczVXWTknmU";

    public Response doGet(String url) {
        if (okHttpClient == null) {
            okHttpClient = new OkHttpClient();
            okHttpClient.setConnectTimeout(5, TimeUnit.SECONDS);
            okHttpClient.setReadTimeout(10,TimeUnit.SECONDS);
        }
        Request request = new Request.Builder().url(url).build();
        Call call = okHttpClient.newCall(request);
        try {
            return call.execute();
        } catch (IOException e) {
            e.printStackTrace();
            return null;
        }
    }

    public Response doPost(String url, String jsonParams) {
        if (okHttpClient == null) {
            okHttpClient = new OkHttpClient();
            okHttpClient.setConnectTimeout(5, TimeUnit.SECONDS);
            okHttpClient.setReadTimeout(10,TimeUnit.SECONDS);
        }
        String sign = MD5.getMD5Str(url + privateKey + jsonParams);
        FormEncodingBuilder formEncodingBuilder = new FormEncodingBuilder();
        formEncodingBuilder.add("param", jsonParams);
        formEncodingBuilder.add("sign", sign);
        formEncodingBuilder.add("publicKey", publicKey);
        RequestBody requestBody = formEncodingBuilder.build();
        Request request = new Request.Builder().url(url).post(requestBody).build();
        Call call = okHttpClient.newCall(request);
        try {
            return call.execute();
        } catch (IOException e) {
            e.printStackTrace();
            return null;
        }
    }

    public Response uploadFile(String url, List<NameValue> files, List<NameValue> params) {
        if (okHttpClient == null) {
            okHttpClient = new OkHttpClient();
        }
        MultipartBuilder multipartBuilder = new MultipartBuilder().type(MultipartBuilder.FORM);
        for (NameValue param : params) {
            multipartBuilder.addFormDataPart(param.getName(), String.valueOf(param.getValue()));
        }
        for (NameValue param : files) {
            multipartBuilder.addFormDataPart(param.getName(), null, RequestBody.create(MediaType.parse("application/octet-stream"), new File(String.valueOf(param.getValue()))));
        }
        RequestBody requestBody = multipartBuilder.build();
        Request request = new Request.Builder().url(url).post(requestBody).build();
        Call call = okHttpClient.newCall(request);
        try {
            return call.execute();
        } catch (IOException e) {
            e.printStackTrace();
            return null;
        }
    }

}
